package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.zhang.mapper.ICustomMapper;
import com.zhang.pojo.CustomBean;
import com.zhang.pojo.SupplierBean;
import com.zhang.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomServiceImpl implements ICustomService {

    @Autowired
    private ICustomMapper customMapper;

    @Override
    public IPage<CustomBean> findByPage(Page<CustomBean> page, String name, String scope) {
        UpdateWrapper<CustomBean> wrapper = new UpdateWrapper<>();
        if (!StringUtils.isNullOrEmpty(name)){
            wrapper.like("c_name",name);
        }if (!StringUtils.isNullOrEmpty(scope)){
            wrapper.like("c_scope",scope);
        }
        return  customMapper.selectPage(page,wrapper);
    }

    @Override
    public List<CustomBean> findAll() {
        return customMapper.selectList(null);
    }

    @Override
    public void add(CustomBean customBean) {
        customMapper.insert(customBean);
    }

    @Override
    public CustomBean findById(int id) {
        return customMapper.selectById(id);
    }

    @Override
    public void update(CustomBean customBean) {
        UpdateWrapper<CustomBean> wrapper = new UpdateWrapper<>();
        wrapper.eq("pk_customId",customBean.getId());
        customMapper.update(customBean,wrapper);
    }
}
